# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :spiral_model.py
# @Time      :2025/7/18 10:03
# @Author    :aiyimu

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.path import Path
import matplotlib.patches as patches
import matplotlib.lines as mlines
import matplotlib as mpl
import matplotlib.font_manager as fm
from matplotlib import rcParams

# ================ 解决中文显示问题 ================
# 方法1：尝试使用系统支持的中文字体
try:
    # 尝试的字体列表（按优先级排序）
    possible_fonts = [
        'SimHei',  # Windows 黑体
        'Microsoft YaHei',  # Windows 微软雅黑
        'STHeiti',  # MacOS 华文黑体
        'Heiti SC',  # MacOS 简体黑体
        'WenQuanYi Zen Hei',  # Linux 文泉驿正黑
        'WenQuanYi Micro Hei',  # Linux 文泉驿微米黑
        'Noto Sans CJK SC'  # Google Noto字体
    ]

    # 检查系统可用的中文字体
    available_fonts = set(f.name for f in fm.fontManager.ttflist)

    # 选择第一个可用的中文字体
    for font_name in possible_fonts:
        if font_name in available_fonts:
            rcParams['font.sans-serif'] = [font_name]
            print(f"使用字体: {font_name}")
            break
    else:
        # 如果找不到系统字体，尝试使用内置字体
        rcParams['font.sans-serif'] = ['SimHei']

    # 解决负号显示问题
    rcParams['axes.unicode_minus'] = False

except Exception as e:
    print(f"设置字体时出错: {e}")
    # 方法2：直接使用字体文件（如果知道路径）
    try:
        # 请根据您的系统修改字体文件路径
        # Windows: 'C:/Windows/Fonts/simhei.ttf'
        # macOS: '/System/Library/Fonts/PingFang.ttc'
        # Linux: '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'
        font_path = 'C:/Windows/Fonts/simhei.ttf'  # 修改为您的实际路径
        font_prop = fm.FontProperties(fname=font_path)
        rcParams['font.family'] = font_prop.get_name()
        print(f"使用字体文件: {font_path}")
    except:
        print("无法加载中文字体，将使用默认字体")

# 设置图形
fig, ax = plt.subplots(figsize=(10, 8))
ax.set_aspect('equal')
plt.title('软件工程螺旋模型', fontsize=15, fontweight='bold')
plt.axis('off')

# 绘制螺旋线
theta = np.linspace(0, 8 * np.pi, 1000)
r = theta * 0.5
x = r * np.cos(theta)
y = r * np.sin(theta)

ax.plot(x, y, 'b-', linewidth=2)

# 添加象限标签
quadrant_labels = [
    "1. 制定计划\n(目标、方案、限制)",
    "2. 风险分析",
    "3. 实施工程\n(开发与测试)",
    "4. 客户评估"
]

angles = [np.pi / 4, 3 * np.pi / 4, 5 * np.pi / 4, 7 * np.pi / 4]
dist = 7

for i, (label, angle) in enumerate(zip(quadrant_labels, angles)):
    x_text = dist * np.cos(angle)
    y_text = dist * np.sin(angle)
    ax.text(x_text, y_text, label,
            ha='center', va='center',
            fontsize=11, bbox=dict(boxstyle='round,pad=0.5',
                                   facecolor='lightyellow',
                                   alpha=0.7))

# 添加迭代标记
iterations = [
    ("初始概念", 0.5),
    ("需求分析", 2.0),
    ("设计", 3.5),
    ("实现", 5.0),
    ("测试与部署", 6.5),
]

angles = [0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi]
for i, (label, r_val) in enumerate(iterations):
    angle = angles[i % len(angles)] + np.pi / 8
    x_point = r_val * np.cos(angle)
    y_point = r_val * np.sin(angle)

    # 绘制迭代点
    ax.plot(x_point, y_point, 'ro', markersize=10)

    # 添加迭代标签
    ax.text(x_point * 1.15, y_point * 1.15, label,
            fontsize=10, ha='center', va='center',
            bbox=dict(boxstyle='round,pad=0.3', facecolor='mistyrose', alpha=0.8))

# 添加中心点（启动）
ax.plot(0, 0, 'gs', markersize=15)
ax.text(0, -0.8, "项目启动", ha='center', va='top', fontsize=10)

# 添加风险分析说明
ax.text(0, -4, "◆ 每个迭代都包含风险评估\n◆ 风险驱动开发过程",
        ha='center', va='center', fontsize=10,
        bbox=dict(boxstyle='round,pad=1', facecolor='honeydew', alpha=0.7))

# 添加图例
legend_elements = [
    mlines.Line2D([], [], color='b', linewidth=2, label='开发迭代路径'),
    mlines.Line2D([], [], marker='o', color='w', markerfacecolor='g',
                  markersize=10, label='项目启动'),
    mlines.Line2D([], [], marker='o', color='w', markerfacecolor='r',
                  markersize=10, label='迭代里程碑')
]
ax.legend(handles=legend_elements, loc='upper right', fontsize=9)

plt.tight_layout()
plt.savefig('spiral_model.png', dpi=300, bbox_inches='tight')  # 保存图片
plt.show()